package org.dandelion.goalTest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;


import org.dandelion.R;
import org.dandelion.goalTest.bean.Course;
import org.dandelion.goalTest.bean.Student;
import org.dandelion.goalTest.bean.Student_Course;
import org.dandelion.goalTest.util.DBHelper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GoalActivity extends AppCompatActivity {
    private EditText editStu_id;
    private EditText editStudent_name, editCourse_id, editCourse_name, editGoal,editCourse_credit,editGoalSeason;
    private RadioButton radioButtonkaoshi;
    private DBHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goal);

        editStu_id = findViewById(R.id.editStu_id);
        editStudent_name = findViewById(R.id.editStudentName);
        editCourse_id = findViewById(R.id.editCourseId);
        editCourse_name = findViewById(R.id.editCourseName);
        radioButtonkaoshi = findViewById(R.id.kaoshi);
        editGoal = findViewById(R.id.editGoal);
        editCourse_credit = findViewById(R.id.editCourse_credit);
        editGoalSeason = findViewById(R.id.editGoalSeason);

        float object = Float.parseFloat("1.0");
        System.out.println(object);

        editGoalSeason.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().length()>1){
                    s.delete(0,1);
                }
            }
        });

        editGoal.addTextChangedListener(new TextWatcher(){
            int l=0;////////记录字符串被删除字符之前，字符串的长度
            int location=0;//记录光标的位置
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                l=s.length();
                location=editGoal.getSelectionStart();
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                Pattern p = Pattern.compile("^(100|[1-9]\\d|\\d)$");

                Matcher m =p.matcher(s.toString());
                if(m.find() || ("").equals(s.toString())){

                }else{
                    Toast.makeText(GoalActivity.this, "请输入正确的数值", Toast.LENGTH_SHORT).show();
                    s.delete(location,location+1);
                }
            }});

        editCourse_credit.addTextChangedListener(new TextWatcher() {
            int l=0;////////记录字符串被删除字符之前，字符串的长度
            int location=0;//记录光标的位置
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                l=s.length();
                location=editGoal.getSelectionStart();
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}

            @Override
            public void afterTextChanged(Editable s) {
                if(s.toString().length()>1){
                    Toast.makeText(GoalActivity.this, "请输入1-5的数值", Toast.LENGTH_SHORT).show();
                    s.delete(location,location+1);
                }
            }
        });

        dbHelper = new DBHelper(this);
    }

    /*
    * TODO:计算学分绩
    *
    *
    * */
    public void calculate(View view){
        /*
        * 不考虑挂科
        * */


        DBHelper dbHelper = new DBHelper(this);

        /*
        * 某学科学分
        * */
        int credit = 0;
        /*
        * 学生某学科的分数
        */
        float goal = 0;
        /*
        * 学生的课程的总学分
        * */
        int sum_credit = 0;
        /*
        * 学生的总（课程学分*课程学分）
        * */
        float sum_goalMultiCredit = 0;
        /*
        * 课程类型
        * */
        int type;
        /*
        * 总的考查课挂科的学分总分
        * */
        int kaocha_credit_loss = 0;
        /*
        * 季度:假定只有一个季度
        * */
        int quarter = 1;
        /*
        * 学分绩计算结果
        * */
        float result;

        String str = editGoalSeason.getText().toString();
        if(str.length()<1){
        }else{
            quarter = Integer.parseInt(str);
        }


        Cursor cursor = dbHelper.selectStudent_Course(Integer.parseInt(editStu_id.getText().toString()));
        while (cursor.moveToNext()){
            credit = cursor.getInt(cursor.getColumnIndex("credit"));
            goal = cursor.getFloat(cursor.getColumnIndex("goal"));
            type = cursor.getInt(cursor.getColumnIndex("type"));
            if(type == 1){
                sum_credit += credit;
                sum_goalMultiCredit += goal * credit;
            }else{
                if(goal < 60){
                    kaocha_credit_loss += credit;
                }
            }
        }

        result = (sum_goalMultiCredit / sum_credit) - (float) kaocha_credit_loss / quarter;

        Toast.makeText(this,String.valueOf(result),Toast.LENGTH_SHORT).show();
    }

    public void searchGoal(View view){
        /*
        * TODO:查询成绩
        * */

        String id = editStu_id.getText().toString();
        if(id.length()<1){
            Toast.makeText(this, "学号不能为空",Toast.LENGTH_SHORT).show();
            return;
        }

        Intent intent = new Intent();
        intent.setClass(this, GoalListActivity.class);
        intent.putExtra("stu_id",Integer.parseInt(id));
        startActivity(intent);
    }

    public void insertGoal(View view){
        /*
         * TODO:新增成绩
         * */
        String stu_id = editStu_id.getText().toString();
        String course_id = editCourse_id.getText().toString();
        String goal = editGoal.getText().toString();
        Student_Course student_course = new Student_Course();

        if(stu_id.length()<1){
            Toast.makeText(this,"学号不能为空",Toast.LENGTH_SHORT).show();
            return;
        }else if(course_id.length()<1){
            Toast.makeText(this,"课程号不能为空",Toast.LENGTH_SHORT).show();
            return;
        }else if(goal.length()<1){
            Toast.makeText(this,"课程分数不能为空",Toast.LENGTH_SHORT).show();
            return;
        }

        student_course.setCourse_id(course_id);
        student_course.setStu_id(Integer.parseInt(stu_id));
        student_course.setGoal(Float.parseFloat(goal));

        dbHelper.insertIntoStudent_Course(student_course);
    }

    public void insertStudent(View v){
        /*
         * TODO:增加学生
         * */
        String id = editStu_id.getText().toString();
        String name = editStudent_name.getText().toString();

        if(id.length()<1){
            Toast.makeText(this,"学号不能为空",Toast.LENGTH_SHORT).show();
            return;
        }else if(name.length()<1){
            Toast.makeText(this,"学生名字不能为空",Toast.LENGTH_SHORT).show();
            return;
        }

        Student student = new Student();
        student.setName(name);
        student.setStu_id(Integer.parseInt(id));
        dbHelper.insertIntoStudent(student);
    }

    public void insertCourse(View view){
        /*
         * TODO:增加课程
         * */
        String id = editCourse_id.getText().toString();
        String name = editCourse_name.getText().toString();
        boolean type = radioButtonkaoshi.isChecked();
        String credit = editCourse_credit.getText().toString();
        if(id.length()<1){
            Toast.makeText(this,"课程号不能为空",Toast.LENGTH_SHORT).show();
            return;
        }else if(name.length()<1){
            Toast.makeText(this,"课程名不能为空",Toast.LENGTH_SHORT).show();
            return;
        }else if(credit.length()<1){
            Toast.makeText(this,"课程学分不能为空",Toast.LENGTH_SHORT).show();
            return;
        }

        Course course = new Course();
        course.setCourse_id(id);
        course.setName(name);
        course.setType(type);
        course.setCredit(Integer.parseInt(credit));

//        SimpleAdapter simpleAdapter new SimpleAdapter();
//        simpleAdapter.
        dbHelper.insertIntoCourse(course);

    }
}